package com.gitee.wsl.ext.list

/**
 * Adds all elements that are not null to the list.
 *
 * Does not create a new list, but adds elements to the existing list.
 *
 * @param elements The elements to add to the list
 */
fun <T> MutableList<T>.addAllNotNull(vararg elements: T?) {
    elements.forEach { if (it != null) add(it) }
}


/**
 * Adds the [element] at [idx] by creating a new list with the element added.
 */
fun <E> List<E>.addAt(idx: Int, element: E) = toMutableList().apply { add(idx, element) }.toList()

/**
 * Adds the [element] at the beginning by creating a new list with the element added.
 */
fun <E> List<E>.prepend(element: E) = addAt(0, element)

fun <T> MutableList<T>.addFirst(element: T) = this.add(0,element)

fun <T> MutableList<T>.addLast(element: T) = this.add(element)